import java.util.*;
class A {
private static int notFinalized;
public static int notFinalized() {return notFinalized;}
private static ArrayList finalizedNames = new ArrayList();
public static Iterator finalizedNames() {
return finalizedNames.iterator();
}
private String name;
public A(String s) {name = s; notFinalized++;}
public void finalize() {
finalizedNames.add(name);
notFinalized--;
}
}
class F {
public static void main(String[] args) {
A x1 = new A("X");
A y1 = new A("Y");
x1= null;
y1 = null;
System.gc();
while (A.notFinalized() > 0) {
try {Thread.sleep(1000);} catch (InterruptedException ie) {}
}
Iterator i = A.finalizedNames();
while (i.hasNext()) {
System.out.print(i.next());
}
}
}
Which of the following are true statements and which of the following could be a result of attempting to compile and run the program?
| a. | Prints: XY |
| b. | Prints: YX |
| c. | Prints: XXYY |
| d. | Prints: YYXX |
| e. | Nothing is printed. |
| f. | There is no guarantee that the garbage collector will run. |
| g. | There is no guarantee that F.main will run to completion. |
| h. | Compiler Error. |
| i. | Run Time Error. |
| j. | None of the above. |
class J {
private static int notFinalized;
public static int notFinalized() {return notFinalized;}
private K k;
private int name;
public int name() {return name;}
public J(K k, int i) {this.k = k; name = i; notFinalized++;}
public void finalize() {
synchronized (k) {
System.out.print(name);
notFinalized--;
k.notify();
}
}
}
class K {
private void m1() {
J j = null;
for (int i = 0; i < 5; i++) {
j = new J(this, i); // 1
}
Runtime.getRuntime().gc(); // 2
synchronized (this) {
while (J.notFinalized() > 0) {
try {wait();} catch (InterruptedException ie) {}
}
}
}
public static void main(String[] args) {
new K().m1();
}
}
When the processing of line 2 begins how many objects of type J that were created at line 1 are eligible for garbage collection?
| a. | 0 |
| b. | 1 |
| c. | 4 |
| d. | 5 |
| e. | Can not be determined without more information. |
| f. | Compiler error. |
| g. | Run time error. |
| h. | None of the above. |
class Arnold {
private H h;
private String quote;
public String quote() {return quote;}
private static int notFinalized;
public static int notFinalized() {return notFinalized;}
public Arnold(H h, String s) {this.h = h; quote = s; notFinalized++;}
public void finalize() {
synchronized (h) {
notFinalized--;
h.arnold(this);
h.notify();
}
}
}
class H {
private Arnold arnold;
public void arnold(Arnold a) {arnold = a;}
public void m1() {
arnold = new Arnold(this, "I'll be back!");
arnold = null;
System.gc();
synchronized (this) {
while (Arnold.notFinalized() > 0) {
try {wait();} catch (InterruptedException ie) {}
}
}
System.out.print(arnold.quote()); // 1
}
public static void main(String[] args) {new H().m1();}
}
Which of the following are true statements and which of the following could be a result of attempting to compile and run the program?
| a. | A finalizer-reachable or finalizable (also known as resurrectable) object can never become reachable again. |
| b. | NullPointerException is thrown at line 1. |
| c. | Prints: I'll be back! |
| d. | Prints nothing. |
| e. | Compiler error. |
| f. | Run time error. |
| g. | None of the above. |
class I {
private static int notFinalized;
public static int notFinalized() {return notFinalized;}
private J observer;
private I other;
public void other(I i) {other = i;}
private String name;
public String name() {return name;}
public I(String s, J j) {this (s, j, null);}
public I(String s, J j, I i) {
name = s;
observer = j;
other = i;
notFinalized++;
}
public void finalize() {
System.out.print(name);
synchronized (observer) {
notFinalized--;
observer.notify();
}
}
}
class J {
private void m1() {
I i1 = new I("X", this);
I i2 = new I("Y", this, i1);
I i3 = new I("Z", this, i2);
i1.other(i3);
i3 = i2 = i1 = null; // 1
Runtime.getRuntime().gc();
synchronized (this) {
while (I.notFinalized() > 0) {
try {wait();} catch (InterruptedException ie) {}
}
}
}
public static void main (String[] args) {
new J().m1();
}
}
Which of the following are true statements and which of the following could be a result of attempting to compile and run the program?
| a. | All three instances of I form a ring where each instance within the ring has a reference to the next instance. |
| b. | All three instances of I are eligible for garbage collection after references i1, i2 and i3 are set to null. |
| c. | There is no guarantee that the garbage collector will run. |
| d. | The order in which the instances in the ring are garbage collected is not guaranteed. |
| e. | The name of all three instances of I might be printed. |
| f. | Compiler error. |
| g. | Run time error. |
| h. | None of the above. |
class J {
private static int finalized;
public static int finalized() {return finalized;}
private K k;
private String name;
public String name() {return name;}
public J(K k, String s) {this.k = k; name = s;}
public void finalize() {
synchronized (k) {
finalized++;
k.j(this);
k.notify();
}
}
}
class K {
private J j1;
public void j(J j) {j1 = j;}
public void m1() {
j1 = new J(this, "X");
j1.finalize();
j1.finalize();
for (int i = 0; i < 3; i++) {
int finalized = J.finalized();
j1= null;
System.gc();
synchronized (this) {
if (J.finalized() == finalized) {
try {wait(1000);} catch (InterruptedException ie) {}
}
}
if (j1 != null) {
System.out.print(j1.name());
}
}
System.out.print(J.finalized());
}
public static void main(String[] args) {new K().m1();}
}
Which of the following are true statements and which of the following could be a result of attempting to compile and run the program?
| a. | A finalizer-reachable or finalizable (also known as resurrectable) object can never become reachable again. |
| b. | The user code can invoke the finalize method more than once. |
| c. | The garbage collector will never call the finalize method more than once. |
| d. | The output could include the character X. |
| e. | The output could include more than one character X. |
| f. | The output could include the number 3. |
| g. | The output could include a number larger than 3. |
| h. | Compiler error. |
| i. | Run time error. |
| j. | None of the above. |
import java.util.*;
class C {
private Object observer;
private static int notFinalized;
public static int notFinalized() {return notFinalized;}
private static ArrayList finalizedNames = new ArrayList();
public static Iterator finalizedNames() {
return finalizedNames.iterator();
}
private String name;
public C(Object obs, String s) {observer = obs; name = s; notFinalized++;}
public void finalize() {
synchronized (observer) {
finalizedNames.add(name);
notFinalized--;
observer.notify();
}
}
}
class G {
public void m1() {
C x1 = new C(this, "X");
C y1 = new C(this, "Y");
x1= null;
y1 = null;
System.gc();
synchronized (this) {
while (C.notFinalized() > 0) {
try {wait();} catch (InterruptedException ie) {}
}
}
Iterator i = C.finalizedNames();
while (i.hasNext()) {
System.out.print(i.next());
}
}
public static void main(String[] args) {
new G().m1();
}
}
Which of the following are true statements and which of the following could be a result of attempting to compile and run the program?
| a. | Prints: XY |
| b. | Prints: YX |
| c. | Prints: XXYY |
| d. | Prints: YYXX |
| e. | Nothing is printed. |
| f. | There is no guarantee that the garbage collector will run. |
| g. | There is no guarantee that G.main will run to completion. |
| h. | Compiler Error. |
| i. | Run Time Error. |
| j. | None of the above. |
class Arnold {
private I i;
private String quote;
public String quote() {return quote;}
private static int instances;
public static int instances() {return instances;}
public static void heIsBack() {instances++;}
public Arnold(I i, String s) {this.i = i; quote = s; instances++;}
public void finalize() {
synchronized (i) {
instances--;
i.arnold(this);
i.notify();
}
}
}
class I {
private Arnold arnold;
public void arnold(Arnold a) {arnold = a;}
public void m1() {
arnold = new Arnold(this, "I'll be back!");
for (int i = 0; i < 3; i++) {
arnold = null;
System.gc();
synchronized (this) {
if (Arnold.instances() > 0) {
try {wait(1000);} catch (InterruptedException ie) {}
}
}
if (arnold != null) {
Arnold.heIsBack();
System.out.print(arnold.quote());
}
}
}
public static void main(String[] args) {new I().m1();}
}
Which of the following are true statements and which of the following could be a result of attempting to compile and run the program?
| a. | A finalizer-reachable or finalizable (also known as resurrectable) object can never become reachable again. |
| b. | The garbage collector will never call the finalize method more than once. |
| c. | Could print Arnold's quote more than once. |
| d. | Can not print Arnold's quote more than once. |
| e. | Could print nothing. |
| f. | Compiler error. |
| g. | Run time error. |
| h. | None of the above. |